/**最初格式
//menu.h (superior
//int superMenu();
//int secondMenu(int flag);

//interface.h (inferior
//static void adminitorInteface(int TorF,N_air* airplaneHead,N_guest* guestHead);
//
//static void guestInteface(int TorF, N_air* airplaneHead,N_guest* guestHead);
//
//static void bookTicketInteface(N_air* airplaneHead,N_guest* guestHead);
//
//static void inferior_bookTicketInteface(N_air* airplaneHead,N_guest* guestHead);

//static void find_AirplaneByCity(N_air* airplaneHead);

*/

//内含 goto 和 递归两种尝试写法
/*/此部分由小组三人共同完成。
周：提供模板和语言命名规则及参数、最终整理优化改善（补丁）+部分3\4\5级菜单
徐、栗：初步代码coding
*/

#ifndef MENU_H_
#define MENU_H_

# include "stdio.h"
# include "stdlib.h"
# include "string.h"
# include "windows.h"

# include "all_struct.h"
# include "display.h"
# include "FindAndLook.h"
# include "admin.h"
# include "guest.h"
# include "judge.h"
# include "tool.h"

//修改航班界面（三级菜单）
int alterAirInterface(int key)
{
	printf("\n----------------------------------------------\n\n");
	fprintf(stdout,"\t\t 1、修改起末城市\n");
	fprintf(stdout,"\t\t 2、修改所在时间\n");
	fprintf(stdout,"\t\t 3、修改所属公司\n");
	fprintf(stdout,"\t\t 4、修改所属型号\n");
	fprintf(stdout,"\t\t 5、修改航班票价\n");
	fprintf(stdout,"\t\t 6、修改票价折扣(0~1)\n");
	fprintf(stdout,"\t\t 7、修改总票量（最大40）\n");
	fprintf(stdout,"\t\t 8、修改取消\n");
	fprintf(stdout,"\t\t 0、修改全部all\n");
	printf("\n----------------------------------------------\n");
	printf("> >请选择：");
	fflush(stdin);
	scanf("%d",&key);

	if (8 < key || key <0)//写成while也行，但没必要。
	{
		fprintf (stdout,"ERROR:指令错误（default），请参考界面选项输入正确指令\n");
		key = alterAirInterface(key);
	}

	return key ;
}

//查询航班主函数jiemian (管理员)（3级
static void find_Airplane(Airplane* airplaneHead)
{
	printf("************************************************************\n");
	printf("************** 1、指定飞机号查询          ******************\n");
	printf("************** 2、指定起飞抵和达城市查询  ******************\n");
	printf("************** 3、指定时间查询            ******************\n");
	printf("************** 4、指定价格查询            ******************\n");
	printf("************** 5、指定总票数查询          ******************\n");
	printf("************** 6、指定折扣查询            ******************\n");
	printf("************** 7、指定remian票数查找      ******************\n");
	printf("************** 8、指定航空公司查询        ******************\n");
	printf("************** 9、指定型号查询            ******************\n");
	printf("************** 10、指定已有乘客人数查询   ******************\n");
	printf("************** 11、查询航班-旅客-信息     ******************\n");
	printf("************** 12、查看所有航班信息       ******************\n");
	printf("************** 13、返回上一级             ******************\n");
	printf("************************************************************\n");
	printf("  请选择 > >  ");

	int TorF;
	int i = 0;//1玩点花活,运用下sprintf 的int性质
	//但主要还是作为一些判别工具存在
	fflush(stdin);
	scanf("%d",&TorF);
	switch(TorF)
	{
		case 1:
			find_AirplaneByNumber(airplaneHead);
			break;
		case 2:
			find_AirplaneByCity(airplaneHead,i);
			break;
		case 3:
			find_AirplaneBytime(airplaneHead,i);
			break;
		case 4:
			find_AirplaneByprice(airplaneHead,i);
			break;
		case 5:
			find_AirplaneBytoatl_tickets(airplaneHead,i);
			break;
		case 6:
			find_AirplaneBydiscount(airplaneHead,i);
			break;
		case 7:
			find_AirplaneByremain_tickets(airplaneHead,i);
			break;
		case 8:
			find_AirplaneBycompany(airplaneHead,i);
			break;
		case 9:
			find_AirplaneBytype(airplaneHead,i);
			break;
		case 10:
			find_AirplaneByguest(airplaneHead,i);
			break;
		case 11:
			find_GuestByFlightNumber(airplaneHead);
			break;
		case 12:
			find_AllAirplane(airplaneHead);
			break;

		case 13:
			return;

		default :
			printf("Error:inviald order , try again！\n\n");
	}

}

//查询航班主函数jiemian (旅客)（4级
static void look_Airplane(N_air* airplaneHead,N_guest* guestHead)
{
	int TorF;
	int i = 0;
	printf("\n----------------------------------------------\n\n");
	printf("\t\t 1、指定飞机号查找航班信息\n");
	printf("\t\t 2、指定起终city查找航班信息\n");
	printf("\t\t 3、指定时间查找航班信息\n");
	printf("\t\t 4、指定价格查找航班信息\n");
	printf("\t\t 5、指定总票数查找航班信息\n");
	printf("\t\t 6、指定折扣查找航班信息\n");
	printf("\t\t 7、指定remian票数查找航班信息\n");
	printf("\t\t 8、指定航空公司查找航班信息\n");
	printf("\t\t 9、指定型号查找航班信息\n");
	printf("\t\t 10、返回上一级\n");
	printf("\n----------------------------------------------\n");
	printf("> >请选择：");

	fflush(stdin);
	scanf("%d",&TorF);
	switch(TorF)
	{
		case 1:
			lookAirplaneByNumber(airplaneHead);
			break;

		case 2:
			lookAirplaneByCity(airplaneHead,i);
			break;

		case 3:
			lookAirplaneBytime(airplaneHead,i);
			break;

		case 4:
			lookAirplaneByprice(airplaneHead,i);
			break;

		case 5:
			lookAirplaneBytoatl_tickets(airplaneHead,i);
			break;

		case 6:
			lookAirplaneBydiscount(airplaneHead,i);
			break;

		case 7:
			lookAirplaneByremain_tickets(airplaneHead,i);
			break;

		case 8:
			lookAirplaneBycompany(airplaneHead,i);
			break;

		case 9:
			lookAirplaneBytype(airplaneHead,i);
			break;

		case 10:
			return;

		default :
			printf("Error:inviald order , try again！\n\n");
	}


}

//订票界面 (旅客) (3级
static void bookTicketInteface(Airplane* airplaneHead,Guest* guestHead,N_guest *current_Guest)
{
	int TorF;
	while (1)
	{
		printf("\n----------------------------------------------\n\n");
		printf("\t\t 1、查看当前所有航班信息\n");
		printf("\t\t 2、指定查找航班信息\n");
		printf("\t\t 3、选择航班订票\n");
		printf("\t\t 4、返回上一级\n");
		printf("\n----------------------------------------------\n");
		printf("> >请选择：");

		fflush(stdin);
		scanf("%d",&TorF);

		switch(TorF)
		{
			case 1:
				lookAllAirplane(airplaneHead);
				break;

			case 2:
				look_Airplane(airplaneHead,guestHead);
				fprintf(stdout,"缓冲中.....\n");
				fprintf(stdout,"（人工暂缓区，虽然非真实状态，但想以此体现对-多线程-的思考和理解）\n");
				Sleep(1000) ;
				system("pause");
				break;

			case 3:
				bookTicket(airplaneHead, current_Guest);
				break;

			case 4:
				return;

			default :
				printf("Error:inviald order , try again！\n\n");
		}

	}

}

//二级菜单(admin/guest)
static int secondMenu(int flag)
{
	if(flag == 1)
	{
		printf("------------------  管理员菜单 ----------------------\n");
		printf("\t 1、录入航班信息  \n");
		printf("\t 2、修改航班信息    \n");
		printf("\t 3、删除航班    \n");
		printf("\t 4、查询航班   \n");
		printf("\t 41、查询所有旅客    \n");
		printf("\t 42、查询特定旅客    \n");
		printf("\t 5、航班排序 （无链表时请勿点击，未作保护处理   \n");
		printf("\t 6、返回上一级 \n");
		printf("\t 7、文件保存信息 \n");
		printf("\t 8、清空航班列表 \n");
		printf("---------------------------------------------------------------\n");
	}
	else if(flag == 2)
	{
		printf("------------------  旅客菜单 ----------------------\n");
		printf("\t 1、查看个人信息    \n");
		printf("\t 2、修改密码    \n");
		printf("\t 3、充值账户    \n");
		printf("\t 4、订票    \n");
		printf("\t 5、退票    \n");
		printf("\t 6、登出（login out) \n");
		printf("\t 0、退出系统 \n");
		printf("---------------------------------------------------------------\n");
	}
	printf("	> >请选择功能：");

	int funChoose;
	fflush(stdin);
	scanf("%d",&funChoose);

	return funChoose;

}

//管理员界面  2
static void adminitorInteface(int TorF,Airplane* airplaneHead,Guest* guestHead)
{
	char account[32];

	int times = 5;
	while(1)
	{
		printf("请输入管理员账号：");
		fflush(stdin);
		gets(account);
		fflush(stdin);

		times--;
		if(0 == strcmp(account,AdminAccount))
		{
			printf("验证成功！ 即将进入管理员菜单界面....\n");
			fprintf(stdout,"加载中，请稍后。。。。\n");

			system("pause");

			break;
		}

		else if(times >= 1)
			printf("账号或密码错误，您还有%d次机会\n\n",times);

		else
		{
			printf("抱歉！试错机会已用完，即将返回主界面......\n");
			system("pause");

			return;

		}

	}

	while(1)
	{
		int funChoose = secondMenu(TorF);
		switch(funChoose)
		{
			case 1:
				insertAirplane(airplaneHead);
				break;
			case 2:
				alterAirplane(airplaneHead);
				break;
			case 3:
				deleteAirplane(airplaneHead);
				break;
			case 4:
				find_Airplane(airplaneHead);
				break;
			case 41:
				find_AllGuest(guestHead,airplaneHead);
				break;
			case 42:
				find_SpecialGuestByID(guestHead,airplaneHead);
				break;
			case 5:
				sort_Airplane(airplaneHead,guestHead);
				break;
			case 6:
				//system("cls");
				return;

			case 7:
				save_byFile(airplaneHead,guestHead);
				printf("文件信息保存成功！");
				system("pause");
				break;

			case 8:
			{

				Airplane* tool = airplaneHead;
				tool->next = NULL;
				free(tool);

				fprintf(stdout,"已全部删除[all_delect]\n");
				system("pause");
				break;
			}

			default :
				printf("Error:inviald order , try again！\n\n");
		}
		//system("cls");
	}

}

//旅客界面 2 TODO:参数还是多了点啊 --zky
static void guestInteface(int TorF,Airplane* airplaneHead,Guest* guestHead,
                          N_guest  *current_Guest,char Guest_UserName[20])
{
	char userName[20];
	char password[30];
	char ToF;//yes or no

	int times = 5;
	while(1)
	{
		printf("请输入用户名：");
		fflush(stdin);
		s_gets(userName,20);
		printf("请输入密码：");
		fflush(stdin);
		s_gets(password,30);
		times--;
		if(NULL != judgeGuestLogin(userName,password,guestHead,Guest_UserName,current_Guest))
		{
			current_Guest = judgeGuestLogin(userName,password,guestHead,Guest_UserName,current_Guest);
			printf("输入正确！ 即将进入用户菜单界面 > >\n");
			strcpy(Guest_UserName,userName);
			system("pause");
			//system("cls");
			break;
		}
		else if(times >= 1)
		{
			printf("用户名或密码错误，您还有%d次机会\n\n",times);

			if (1 == judgeUserNameExsit(guestHead, userName) )
			{
				Guest* p = guestHead->next;
				while(p)
				{
					if(!strcmp(p->userName,userName) )
						current_Guest = p; //xunzhao 链表对应位次

					p = p->next;
				}

				printf("忘记密码？是否修改密码？ (y:Yes/n:No)： ");
				fflush(stdin);
				scanf("%c",&ToF);
				if(ToF == 'y' || ToF == 'Y')
					alterpassword (current_Guest);

			}

		}
		else
		{
			printf("抱歉！试错机会已用完，即将返回主界面......\n");
			system("pause");
			//system("cls");
			return;
		}
	}
	while(1)
	{
		int funChoose = secondMenu(TorF);
		switch(funChoose)
		{
			case 1:
				checkPersonalInfo(airplaneHead,current_Guest);
				break;
			case 2:
				alterpassword(current_Guest);
				break;
			case 3:
				accountRecharge(guestHead,current_Guest);
				break;
			case 4:
				bookTicketInteface(airplaneHead,guestHead,current_Guest);
				break;
			case 5:
				refundTicket(airplaneHead,guestHead,current_Guest);
				break;
			case 6:
				//system("cls");
				return;

			case 0:
				save_byFile(airplaneHead,guestHead);
				save_byFile_1(airplaneHead,guestHead);
				printf("欢迎下次使用！\n");
				empty_LinkedList(airplaneHead,guestHead);
				system ("pause");
				exit(0);

			default :
				printf("Error:inviald order , try again！\n\n");

		}

		//system("cls");
	}


}

//主菜单  1
static int superMenu()
{
	printf("------------------  欢迎进入飞机订票系统  ----------------------\n\n");
	printf("\t\t 1、管理员登录\n\n");
	printf("\t\t 2、旅客登录\n\n");
	printf("\t\t 3、账号注册\n\n");//注册旅客(个人)信息
	printf("\t\t 4、文件保存\n\n");//(课设备用键)文件保存信息
	printf("\t\t 0、退出系统(auto_copy)\n\n");
	printf("---------------------------------------------------------------\n");

	printf("	> >请选择主功能：");
	int TorF;
	scanf("%d",&TorF);
	return TorF;
}

# endif